package com.ciwong.epaper.modules.evaluate.ui;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.evaluate.util.SpeechUtils;
import com.ciwong.epaper.widget.PlayStateDubView;
import com.ciwong.epaper.widget.RoundProgressBar;

import android.os.Handler;

/**
 * Created by young on 2017/4/19.
 * 单词跟读底部控件
 */
public class WordBottomView extends LinearLayout {
    private RoundProgressBar round_progress_bar;
    private ImageView iv_playing, iv_recording, iv_start_pas_iocn;
    private View show_record_tip, finsh_record;
    private PlayStateDubView play_dubing_iocn;
    private ImageView play_iocn, img_stop_recording;
    public static final int UPDATAPROGRESS = 1;
    protected static final int RECORD_ADD_DURATION = 1000;// 录音增加的时长/完成录音显示时间
    protected Handler mHandler = new Handler();
    private Runnable runnable;


    public WordBottomView(Context context) {
        super(context);
        initView();
    }

    public WordBottomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WordBottomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(getResources().getColor(R.color.white));
        View.inflate(getContext(), R.layout.word_bottom, this);
        round_progress_bar = findViewById(R.id.round_progress_bar);
        iv_playing = findViewById(R.id.problemPlay);
        iv_recording = findViewById(R.id.img_micro_recording);
        iv_start_pas_iocn = findViewById(R.id.iv_stopOrplay);
        show_record_tip = findViewById(R.id.show_record_tip);
        finsh_record = findViewById(R.id.finsh_record);
        play_dubing_iocn = findViewById(R.id.play_dubing_iocn);
        play_iocn = findViewById(R.id.play_iocn);
        img_stop_recording = findViewById(R.id.img_stop_recording);
    }


    /**
     * 设置播放图标的显示状态
     *
     * @param isShow
     */
    public void setPlayBtnState(boolean isShow) {
        if (iv_playing == null) {
            return;
        }
        if (isShow) {
            iv_playing.setVisibility(View.VISIBLE);
            Drawable drawable = iv_playing.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).start();
            }

        } else {
            iv_playing.setVisibility(View.GONE);
            Drawable drawable = iv_playing.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).stop();
            }
        }
    }

    /**
     * 设置手动播放图标的显示状态
     *
     * @param isPlay
     */
    public void setPlayWordState(boolean isPlay) {
        if (play_iocn == null) {
            return;
        }
        if (isPlay) {
            play_iocn.setVisibility(View.VISIBLE);
            Drawable drawable = play_iocn.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).start();
            }

        } else {
            Drawable drawable = play_iocn.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).stop();
            }
        }
    }

    /**
     * 设置录音图标的显示状态
     *
     * @param isRecording 是否是录音
     * @param isUnAuto    是否是手动模式
     */
    public void setRecordState(boolean isRecording, boolean isUnAuto) {

        if (null == iv_recording) {
            return;
        }

        if (isRecording) {
            if (isUnAuto) {
                iv_recording.setVisibility(GONE);
                img_stop_recording.setVisibility(VISIBLE);
            } else {
                runnable = new Runnable() {
                    @Override
                    public void run() {
                        finsh_record.setVisibility(VISIBLE);
                    }
                };
                mHandler.postDelayed(runnable, RECORD_ADD_DURATION);
                iv_recording.setVisibility(VISIBLE);
                img_stop_recording.setVisibility(GONE);
                Drawable drawable = iv_recording.getDrawable();
                if (drawable != null && drawable instanceof AnimationDrawable) {
                    ((AnimationDrawable) drawable).start();
                }
            }
        } else {
            if (runnable != null) {
                mHandler.removeCallbacks(runnable);
            }
            if (isUnAuto) {
                iv_recording.setVisibility(GONE);
                img_stop_recording.setVisibility(GONE);
                iv_recording.setVisibility(VISIBLE);
            } else {
                finsh_record.setVisibility(GONE);
                iv_recording.setVisibility(GONE);
                Drawable drawable = iv_recording.getDrawable();
                if (drawable != null && drawable instanceof AnimationDrawable) {
                    ((AnimationDrawable) drawable).stop();
                }
            }
        }
    }

    public void setRoundprogressMax(int max) {
        round_progress_bar.setMax(max);
    }

    public void setRoundProgress(int progress) {
        round_progress_bar.setProgress(progress);
    }

    //播放录音倒计时
    public void setRoundProgressAndMax(int max, int p) {
        setRoundprogressMax(max);
        setRoundProgress(p);
    }

    //暂停播放按钮的状态
    public void setPlayOrPause(boolean isShowStop) {
        if (isShowStop) {
            iv_start_pas_iocn.setImageResource(R.mipmap.type_time_out);
        } else {

            iv_start_pas_iocn.setImageResource(R.mipmap.play);
        }
    }

    //设置倒计时图标显示状态
    public void setRoundRrogressBarState(boolean isShow) {
        if (isShow) {
            round_progress_bar.setVisibility(View.VISIBLE);
        } else {
            round_progress_bar.setVisibility(View.GONE);
        }
    }

    /*
     *初始状态
     */
    public void setInitState(boolean isUnAuto) {
        setRoundRrogressBarState(false);
        setPlayBtnState(false);
        setRecordState(false, isUnAuto);
    }

    //设置播放状态下的图标显示
    public void payWordsState(boolean isUnAuto) {
        if (isUnAuto) {
            //手动模式
            setPlayWordState(true);
        } else {
            //自动模式
            setRoundRrogressBarState(true);
            setRecordState(false, isUnAuto);
            setPlayBtnState(true);
        }
    }

    //播放提示音时图标状态  或者   正常播放时无图标页面
    public void playHintState(boolean isUnAuto) {
        setRoundRrogressBarState(false);
        setRoundProgress(0);
        setPlayBtnState(false);
        setRecordState(false, isUnAuto);
    }

    //录音状态图标状态
    public void recordingState(boolean isUnAuto) {
        setPlayBtnState(false);
        setRecordState(true, isUnAuto);
        setRoundProgress(0);
        setRoundRrogressBarState(true);
    }

    //设置播放状态
    public void setPlayState(boolean isUnAuto, int readMode) {
        if (!isUnAuto) {
            play_iocn.setVisibility(GONE);
            iv_playing.setVisibility(GONE);
            iv_recording.setVisibility(GONE);
            img_stop_recording.setVisibility(GONE);
        } else {
            if (readMode == SpeechUtils.WordReadMode.READ_REPEAT) {
                play_iocn.setVisibility(View.VISIBLE);
            } else {
                play_iocn.setVisibility(View.GONE);
            }
            iv_playing.setVisibility(GONE);
            iv_recording.setVisibility(VISIBLE);
            img_stop_recording.setVisibility(GONE);
        }
    }

    //自动更新进度
    public void autoUpdateProgress(final int timer) {
        round_progress_bar.setTimer(timer);
    }

    public View getFinishRecordView() {
        return finsh_record;
    }

    public View getRecordTipView() {
        return show_record_tip;
    }
}
